/**
 * @Date:   2018-09-20T14:18:51+08:00
 * @Filename: index.js
 * @Last modified time: 2018-09-23T00:22:55+08:00
 */

// index: 登录页面

const User = require('../model/User')
const md5 = require('md5')
module.exports = {
	'GET /': async (ctx, next) => {
		let user = new User()
		user.debug = false
		// let all = await user.saveAll([
		// 	{username: '524314430@qq.com',password: md5('123456')},
		// 	{username: '524314430@qq.com',password: md5('123456')}
		// ])
		let _save = await user.save({username:'jswei30@qq.com',password:md5('123456')})
		console.log(_save)
		let u = await user.find(22)
		let _d = await user.selectByFiled({id:23})
		let count = await user.count()
		let page = await user.pagination(1,4,'','id,username,create_date')
		// let _update = await user.update({username:'jswei_30@gmail.com',password:md5('1234567')},['id','eq',22])
		// console.log(_update)
		// let _update = await user.updateByField({username:'524314430@qq.com',id:23})
		// console.log(_update)
		// let _s = await user.save({username: '524314430@qq.com',password: md5('123456')})
		// 使用连接池
		// user.getPoolConnection().then(res => {
		// 	user.save({username: '524314430@qq.com',password: md5('123456')}).then(res1 => {
		// 		res.release() //关闭连接
		// 		console.log('Insert Id:', res1)
		// 	}).catch(_ => {})
		// })
		let a = ctx.cookies.get('cid')
		if (a == 'helloworld') {
			ctx.render('signin-ok.html', {
				title: '登录成功',
				name: email
			});
		} else {
			ctx.render('index.html', {
				title: 'KOA',
				count: count,
				page: JSON.stringify(page),
				user: JSON.stringify(u),
				d: JSON.stringify(_d)
			});
		}
	}
};
